# Slide Rule
# Copyright (C) 2026  Jeffry Johnston
#
# This file is part of Slide Rule.
#
# Slide Rule is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Slide Rule is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Slide Rule.  If not, see <https://www.gnu.org/licenses/>.

name := Rule
long_name := "Slide Rule"
version := 01.00

headers := src/rule.h

srcs :=				\
	display.c		\
	keyboard.c		\
	main.c			\
	mono.c			\
	rule.c			\

common_images :=		\
	labels.png		\
	help_std.png		\
	font_rule.png		\

fx_images := $(common_images)	\
	help_slim.png		\

cg_images := $(common_images)	\
	help_cg100.png		\

.PHONY: all
all: fx fxg3a

FX_CC := sh-elf-gcc
FX_CFLAGS := -DFX9860G -DTARGET_FX9860G -m3 -mb -ffreestanding -nostdlib \
	-Wa,--dsp -Wall -Wextra -std=c11 -g -Os -fstrict-volatile-bitfields
FX_LDFLAGS := -nostdlib -Wl,--no-warn-rwx-segments -T fx9860g.ld
fx_add_in := build_fx/$(name).g1a
fx_bin := build_fx/$(name).bin
fx_elf := build_fx/$(name).elf
fx_objs := $(srcs:%=build_fx/%.o) $(fx_images:%=build_fx/%.o)
fx_libs := $(shell $(FX_CC) -print-file-name=libgint-fx.a) \
	$(shell $(FX_CC) -print-file-name=libc.a) -lgint-fx -lm -lopenlibm -lc -lgcc
fx_icon := assets/icon.png

.PHONY: fx
fx: $(fx_add_in)

$(fx_add_in): $(fx_bin) $(fx_icon)
	fxgxa --g1a -n $(name) -i $(fx_icon) --version="$(version)" -o $@ $<
	du -b $(fx_add_in)

$(fx_bin): $(fx_elf)
	sh-elf-objcopy -O binary -R .bss -R .gint_bss $< $@

$(fx_elf): $(fx_objs)
	$(FX_CC) $(FX_LDFLAGS) -o $@ $^ $(fx_libs) $(fx_libs)

build_fx/%.c.o: src/%.c $(headers) Makefile
	$(FX_CC) $(FX_CFLAGS) -c -o $@ $<

build_fx/%.png.o: assets/%.png assets/fxconv-metadata.txt Makefile
	fxconv --toolchain=sh-elf --fx -o $@ $<

FXG3A_CC := sh-elf-gcc
FXG3A_CFLAGS := -DFXCG50 -DFX9860G_G3A -m4-nofpu -mb -ffreestanding -nostdlib \
	-Wa,--dsp -Wall -Wextra -std=c11 -g -Os -fstrict-volatile-bitfields
FXG3A_LDFLAGS := -nostdlib -Wl,--no-warn-rwx-segments -T fxcg50.ld
fxg3a_add_in := build_fxg3a/$(name).g3a
fxg3a_bin := build_fxg3a/$(name).bin
fxg3a_elf := build_fxg3a/$(name).elf
fxg3a_objs := $(srcs:%=build_fxg3a/%.o) $(cg_images:%=build_fxg3a/%.o)
fxg3a_libs := $(shell $(FXG3A_CC) -print-file-name=libgint-fxg3a.a) \
	$(shell $(FXG3A_CC) -print-file-name=libc.a) -lgint-fxg3a -lm -lopenlibm -lc -lgcc
fxg3a_icon_uns := assets/icon_uns.png
fxg3a_icon_sel := assets/icon_sel.png

.PHONY: fxg3a
fxg3a: $(fxg3a_add_in)

$(fxg3a_add_in): $(fxg3a_bin) $(fxg3a_icon_uns) $(fxg3a_icon_sel)
	fxgxa --g3a -n $(long_name) --icon-uns $(fxg3a_icon_uns) --icon-sel $(fxg3a_icon_sel) --version="$(version)" -o $@ $<
	du -b $(fxg3a_add_in)

$(fxg3a_bin): $(fxg3a_elf)
	sh-elf-objcopy -O binary -R .bss -R .gint_bss $< $@

$(fxg3a_elf): $(fxg3a_objs)
	$(FXG3A_CC) $(FXG3A_LDFLAGS) -o $@ $^ $(fxg3a_libs) $(fxg3a_libs)

build_fxg3a/%.c.o: src/%.c $(headers) Makefile
	$(FXG3A_CC) $(FXG3A_CFLAGS) -c -o $@ $<

build_fxg3a/%.png.o: assets/%.png assets/fxconv-metadata.txt Makefile
	fxconv --toolchain=sh-elf --fx -o $@ $<

$(shell mkdir -p build_fx build_fxg3a)

# Install on Casio fx-9750/9860G/9860G Slim/9860GII
.PHONY: install_p7
install_p7: $(fx_add_in)
	p7 send $(fx_add_in)

# Install on Casio fx-9750/9860 GIII
.PHONY: install_fx
install_fx: $(fx_add_in)
	@while true; do \
	    DEVICE=`blkid | grep LABEL_FATBOOT=\"CASIO\" | cut -d':' -f1`; \
	    [ -n "$$DEVICE" ] && break; \
	    sleep 0.25; \
	done; \
	while true; do \
	    DEST=`findmnt -nr -o target $$DEVICE`; \
	    [ -n "$$DEST" ] && break; \
	    sleep 0.25; \
	done; \
	rm -f "$$DEST/"$(name).g1a; \
	cp -v $(fx_add_in) "$$DEST"/; \
	eject "$$DEST" 2>/dev/null || true

# Install on Casio fx-CG50
.PHONY: install_cg
install_cg: $(fxg3a_add_in)
	@while true; do \
	    DEVICE=`blkid | grep LABEL_FATBOOT=\"CASIO\" | cut -d':' -f1`; \
	    [ -n "$$DEVICE" ] && break; \
	    sleep 0.25; \
	done; \
	while true; do \
	    DEST=`findmnt -nr -o target $$DEVICE`; \
	    [ -n "$$DEST" ] && break; \
	    sleep 0.25; \
	done; \
	rm -f "$$DEST/"$(name).g3a; \
	cp -v $(fxg3a_add_in) "$$DEST"/; \
	eject "$$DEST" 2>/dev/null || true

.PHONY: clean
clean:
	$(RM) -r build_fx/ build_fxg3a/
